/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.ICompleteListener;
import listener.IKeyListener;
import model.ErrorMessage;
import ui.DateFieldEditor;
import ui.Grid;
import ui.GridItem;
import ui.List;
import ui.ListItem;

/**
 *
 * @author kien.tran
 */
public class MainForm extends AForm implements ICommandListener, IKeyListener, ICompleteListener {

    private Grid grid;
    private List list;
    private DateFieldEditor dateEditor;
    private String menuView;

    public MainForm() {
    }

    public MainForm(MStockMidlet control, Display display) {
        super(control, display);
    }

    protected void initData() {
        lang = new String[]{
                    "Vé số chiều sổ",
                    "Dò số nhanh",
                    "Xem theo ngày",
                    "Xem theo Tỉnh/TP",
                    "Thống kê - Phân tích",
                    "Lịch mở thưởng",
                    "Gửi đến bạn bè",
                    "Tùy chọn",
                    "Giúp đỡ",
                    "Chọn",
                    "Thoát",
                    "Xổ số 3 miền"};
  
        recycle = control.getRecycle();
        menuView = control.getMenuView();

    }

    protected void initGui() {
        this.ok = new Command(lang[9], Command.OK);
        this.back = new Command(lang[10], Command.EXIT);

        if (menuView.equals(MStockMidlet.DISPLAY_LIST)) {
            this.list = new List(display, control, lang[11], background, icon);

            list.setSelectCommand(ok);
            list.setCancelCommand(back);
            list.setCommandListener(this);
            list.setKeyListener(this);

            for (int i = 0; i < 9; i++) {
                list.addItem(new ListItem("list" + i, lang[i], control.getIcons()[i]));
            }

            list.show();

        } else if (menuView.equals(MStockMidlet.DISPLAY_GRID)) {
            this.grid = new Grid(display, control, lang[0], background, icon);

            grid.setAutoRepaint(false);
            grid.setColumns(3);

            grid.setSelectCommand(ok);
            grid.setCancelCommand(back);
            grid.setCommandListener(this);
            grid.setKeyListener(this);

            GridItem gi = null;
            for (int i = 0; i < 9; i++) {
                gi = new GridItem("grid" + i, control.getImages()[i], null);
                grid.addItem(gi);
            }

            grid.setSelectedIndex(0);
            grid.show();
        }
    }

    public void commandAction(Command command) {
        if (command == ok) {
            control.getBackManager().push(this.getClass().getName());
            // reset some recycle data
            recycle.empty();

            int index = -1;
            if (list != null) {
                index = list.getSelectedIndex();

            } else if (grid != null) {
                index = grid.getSelectedIndex();
            }

            switch (index) {
                case 0:
                    break;

                case 1:
                    break;

                case 2:
                    break;

                case 3:
                    break;

                case 4:
                    break;

                case 5:
                    break;

                case 6:
                    break;

                case 7:
                    new ConfigForm(control, display, State.CONFIG, State.NONE).show();
                    break;

                case 8:
                    new HelpForm(control, display, State.HELP, State.NONE).show();
                    break;
            }

        } else if (command == back) {
            control.getBackManager().push(this.getClass().getName());
            new ConfirmForm(control, display, State.EXIT, State.NONE).show();
        }
    }

    public void keyAction(int key) {
        if (list != null) {
            if (key == List.FIRE) {
                commandAction(ok);
            }

        } else if (grid != null) {
            if (key == Grid.FIRE) {
                commandAction(ok);
            } else {
                int index = grid.getSelectedIndex();
                grid.setCaption(lang[index]);
                grid.repaint();
            }
        }
    }

    protected void loadData() {
    }

    protected void showData() {
    }

    protected void showError(ErrorMessage error) {
    }

    public void completedAction(Object item, Object result) {
        
    }
}
